<?php


namespace api\modules\v1\controllers\buss;

use api\controllers\OnAuthController;
use api\modules\v1\forms\buss\TargetContentForm;
use common\helpers\CommonHelper;
use services\buss\TargetContentService;
use Yii;


class TargetContentController extends OnAuthController
{
    public $modelClass = '';

    /**
     * @api {get} /v1/buss/target-content/list  选题列表
     * @apiName list
     * @apiGroup TargetContent
     * @apiUse CommonParam
     *
     * @apiParam {String} created_stime     创建开始时间【格式：YYYY-MM-DD】
     * @apiParam {String} created_etime     创建结束时间【格式：YYYY-MM-DD】
     * @apiParam {String} keywords          关键词
     * @apiParam {String} platform_name     平台
     * @apiParam {Number} t_id              账号类型ID
     * @apiParam {Number} c_id              分类
     * @apiParam {Number} created_member_id 创建人ID
     * @apiParam {String} order_column      排序字段
     * @apiParam {String} order_sort        排序值【asc正序、desc倒序】
     * @apiParam {String} page              页码
     * @apiParam {String} pageSize          每页展示数
     *
     * @apiSuccess {Object}  data                       返回数据
     * @apiSuccess {Number}  data.page                  页码
     * @apiSuccess {Number}  data.pageSize              每页展示数
     * @apiSuccess {Number}  data.total                 总数
     * @apiSuccess {Array}   data.list                  列表数据
     * @apiSuccess {Number}  data.list.id               ID
     * @apiSuccess {String}  data.list.title            标题
     * @apiSuccess {String}  data.list.url              网址
     * @apiSuccess {String}  data.list.account_name     昵称
     * @apiSuccess {Array}   data.list.t_name           类型
     * @apiSuccess {String}  data.list.c_name           分类
     * @apiSuccess {Array}   data.list.platform_name    平台
     * @apiSuccess {Number}  data.list.like_num         点赞
     * @apiSuccess {Number}  data.list.comment_num      评论
     * @apiSuccess {Number}  data.list.collect_num      收藏
     * @apiSuccess {Number}  data.list.forward_num      转发
     * @apiSuccess {String}  data.list.topic            话题
     * @apiSuccess {String}  data.list.created_member_name  创建人
     * @apiSuccess {String}  data.list.created_at_str   创建时间
     * @apiSuccess {String}  data.list.change_date      选题更新时间
     */
    public function actionList()
    {
        $params = Yii::$app->request->get();

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        $service = new TargetContentService();
        // 查询结果
        $data = $service->getListPage($params, (int)$page, (int)$pageSize);
        // 格式化数据
        $data['list'] = $service->getListPageFormat($data['list']);
        return $data;
    }

    /**
     * @api {get} /v1/buss/target-content/list-condition  列表筛选条件
     * @apiName list-condition
     * @apiGroup TargetContent
     * @apiUse CommonParam
     *
     * @apiSuccess {Object}  data                   返回数据
     * @apiSuccess {Array}   data.platform          平台【{value:'0', label: '项目'}】
     * @apiSuccess {Array}   data.type              类型【{value:'0', label: '普通'}】
     * @apiSuccess {Array}   data.created_member    创建人
     * @apiSuccess {Array}   data.classify          分类
     */
    public function actionListCondition()
    {
        $params = Yii::$app->request->get();

        $service = new TargetContentService();
        return $service->getSelectCondition();
    }

    /**
     * @api {get} /v1/buss/target-content/info 选题详情
     * @apiName info
     * @apiGroup TargetContent
     * @apiUse CommonParam
     *
     * @apiParam {Number} id 选题ID【新增ID不传，编辑必传ID】
     *
     * @apiSuccess {Object} data                    返回数据
     * @apiSuccess {Number} data.id                 ID
     * @apiSuccess {String} data.title              标题
     * @apiSuccess {String} data.url                网址
     * @apiSuccess {String} data.account_name       昵称
     * @apiSuccess {Number} data.t_id               类型
     * @apiSuccess {Number} data.c_id               分类
     * @apiSuccess {String} data.platform_name      平台
     * @apiSuccess {Number} data.like_num           点赞
     * @apiSuccess {Number} data.comment_num        评论
     * @apiSuccess {Number} data.collect_num        收藏
     * @apiSuccess {Number} data.forward_num        转发
     * @apiSuccess {String} data.topic              话题
     * @apiSuccess {Object} data.condition          筛选条件
     * @apiSuccess {Array}  data.condition.platform 平台
     * @apiSuccess {Array}  data.condition.type     类型
     * @apiSuccess {Array}  data.condition.classify 分类
     */
    public function actionInfo()
    {
        $params = Yii::$app->request->get();
        $id = $params['id'] ?? 0;

        // 查询结果
        $service = new TargetContentService();
        return $service->getInfo((int)$id);
    }

    /**
     * @api {get} /v1/buss/target-content/download-tmp-execl 下载模板文件
     * @apiName download-tmp-execl
     * @apiGroup TargetContent
     * @apiUse CommonParam
     *
     *
     * @apiSuccess {Object} data                    返回数据
     */
    public function actionDownloadTmpExecl()
    {
        $params = Yii::$app->request->get();

        // 查询结果
        $service = new TargetContentService();
        $service->createTmpExecl();
    }

    /**
     * @api {post} /v1/buss/target-content/create 新增选题
     * @apiName create
     * @apiGroup TargetContent
     * @apiUse CommonParam
     *
     * @apiBody {String} title              标题
     * @apiBody {String} url                网址
     * @apiBody {String} account_name       昵称
     * @apiBody {Number} t_id               类型
     * @apiBody {String} platform_name      平台
     * @apiBody {Number} like_num           点赞
     * @apiBody {Number} comment_num        评论
     * @apiBody {Number} collect_num        收藏
     * @apiBody {Number} forward_num        转发
     * @apiBody {Number} c_id               分类
     * @apiBody {String} topic              话题
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         选题ID
     */
    public function actionCreate()
    {
        $params = Yii::$app->request->post();

        $model = new TargetContentForm();
        $model->setScenario('create');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $service = new TargetContentService();
        return $service->create($model);
    }

    /**
     * @api {post} /v1/buss/target-content/update 编辑选题
     * @apiName update
     * @apiGroup TargetContent
     * @apiUse CommonParam
     *
     * @apiBody {Number} id                 ID
     * @apiBody {String} title              标题
     * @apiBody {String} url                网址
     * @apiBody {String} account_name       昵称
     * @apiBody {Number} t_id               类型
     * @apiBody {String} platform_name      平台
     * @apiBody {Number} like_num           点赞
     * @apiBody {Number} comment_num        评论
     * @apiBody {Number} collect_num        收藏
     * @apiBody {Number} forward_num        转发
     * @apiBody {Number} c_id               分类
     * @apiBody {String} topic              话题
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         选题ID
     */
    public function actionUpdate()
    {
        $params = Yii::$app->request->post();

        $model = new TargetContentForm();
        $model->setScenario('edit');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $service = new TargetContentService();
        return $service->edit($model);
    }

    /**
     * @api {post} /v1/buss/target-content/delete 删除选题
     * @apiName delete
     * @apiGroup TargetContent
     * @apiUse CommonParam
     *
     * @apiBody {Number} id                选题ID
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         选题ID
     */
    public function actionDelete()
    {
        $params = Yii::$app->request->post();

        $model = new TargetContentForm();
        $model->setScenario('delete');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $service = new TargetContentService();
        return $service->delete($model);
    }

    /**
     * @api {post} /v1/buss/target-content/upload-execl    导入模板文件【批量上传】
     * @apiName upload-execl
     * @apiGroup TargetContent
     * @apiUse CommonParam
     *
     * @apiParam {File} file                文件【支持：xls、xlsx】
     *
     * @apiSuccess {Object} data            返回内容
     */
    public function actionUploadExecl()
    {
        $service = new TargetContentService();
        return $service->uploadExecl();
    }
}
